package khleuven.mondzorg.db.readers;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.json.JSONException;
import org.json.JSONObject;

import khleuven.mondzorg.Logger;
import khleuven.mondzorg.db.DBException;
import khleuven.mondzorg.domain.EntryType;
import khleuven.mondzorg.domain.JsonKeysDictionary;

public class EntryBuilderOffline extends EntryBuilder {

	protected EntryBuilderOffline() throws DBException {
		super();
	}

	@Override
	protected void reloadAllIllustrationsFromSource() throws DBException {
		FileInputStream fis = EntryType.ILLUSTRATION.getFileInputStream();
		InputStream e = new BufferedInputStream(fis);
		BufferedReader reader;
		try {
			reader = new BufferedReader(new InputStreamReader(e,"UTF-8"),8);
			StringBuilder result = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				result.append(line + "\n");
			}
			e.close();
			reader.close();
			Logger.logDB("Is read for ILLUSTRATION:\n"+result.toString());
			JSONObject illustrationobject = new JSONObject(result.toString());
			super.createEntriesFromJSonArray(illustrationobject.getJSONArray(JsonKeysDictionary.JSON_KEY_GS_ENTRY), EntryType.ILLUSTRATION, EntryType.ILLUSTRATION.getConstructor());
		} catch (UnsupportedEncodingException e1) {
			throw new DBException("reloadAllIllustrationsFromSource",e1);
		} catch (IOException e1) {
			throw new DBException("reloadAllIllustrationsFromSource",e1);
		} catch (JSONException e1) {
			throw new DBException("reloadAllIllustrationsFromSource",e1);
		}
	}

	@Override
	protected void reloadAllEntriesFromSource() throws DBException {
		for(EntryType entrytype: EntryType.values()){
			if(entrytype != EntryType.ILLUSTRATION){
				FileInputStream fis = entrytype.getFileInputStream();
				InputStream e = new BufferedInputStream(fis);
				BufferedReader reader;
				try {
					reader = new BufferedReader(new InputStreamReader(e,"UTF-8"),8);
					StringBuilder result = new StringBuilder();
					String line = null;
					while ((line = reader.readLine()) != null) {
						result.append(line + "\n");
					}
					e.close();
					reader.close();
					Logger.logDB("Is read for "+entrytype.toString()+":\n"+result.toString());
					JSONObject illustrationobject = new JSONObject(result.toString());
					super.createEntriesFromJSonArray(illustrationobject.getJSONArray(JsonKeysDictionary.JSON_KEY_GS_ENTRY), entrytype, entrytype.getConstructor());
				} catch (UnsupportedEncodingException e1) {
					throw new DBException("reloadAllIllustrationsFromSource",e1);
				} catch (IOException e1) {
					throw new DBException("reloadAllIllustrationsFromSource",e1);
				} catch (JSONException e1) {
					throw new DBException("reloadAllIllustrationsFromSource",e1);
				}
			}
		}
		
	}

}
